package com.sdkx.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.entity.CustomAirPort;
import com.sdkx.app.entity.CustomWayPoint;

import java.util.List;


public class WaypointMenuAdapter extends BaseAdapter {
    private Context context;
    private List<Object> list;
    public int clickPosition = -1;
    public AnimationSet set;
    public int indexpostion_animation=-1;
    public WaypointMenuAdapter(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final MyViewHolder vh;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.grid_item, null);
            vh = new MyViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (MyViewHolder) convertView.getTag();
        }
        Object object=list.get(position);
        if(object instanceof CustomWayPoint){
            CustomWayPoint cwp = (CustomWayPoint) object;
            vh.text.setText(cwp.getName());
        }else if(object instanceof CustomAirPort){
            CustomAirPort cap = (CustomAirPort) object;
            vh.text.setText(cap.getName());
        }

        if(clickPosition==position){
            vh.text.setTextColor(Color.RED);
            System.out.println(clickPosition);
        }else{
            vh.text.setTextColor(Color.WHITE);
        }
        if(indexpostion_animation==position){
            if(getSet()!=null){
                vh.itemView.startAnimation(getSet());
            }
        }
        return convertView;
    }

    public void ChangeAdapterStyle(int position) {
        setClickPosition(position);
        notifyDataSetChanged();
    }

    public int getIndexpostion_animation() {
        return indexpostion_animation;
    }

    public void setIndexpostion_animation(int indexpostion_animation) {
        this.indexpostion_animation = indexpostion_animation;
    }

    public void startItemSelectAnimation(AnimationSet set, int indexpostion_animation) {
        setSet(set);
        setIndexpostion_animation(indexpostion_animation);
        notifyDataSetChanged();
    }

    public AnimationSet getSet() {

        return set;
    }

    public void setSet(AnimationSet set) {
        if(set!=null){
            set.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                  //动画结束
                    setIndexpostion_animation(-1);
                    setSet(null);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
        this.set = set;
    }

    public static class MyViewHolder {
        public TextView text;
        public View itemView;

        public MyViewHolder(View itemView) {
            this.itemView = itemView;
            text = (TextView) itemView.findViewById(R.id.textView);
        }
    }

    public int getClickPosition() {
        return clickPosition;
    }

    public void setClickPosition(int clickPosition) {
        this.clickPosition = clickPosition;
    }
}
